package com.banyou.banyou.util;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.login.CityBean;
import com.banyou.banyou.login.regist.RegistActivity;
import com.socks.library.KLog;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;
import cn.qqtheme.framework.picker.AddressPicker;
import cn.qqtheme.framework.util.ConvertUtils;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 项目名称：BanYou
 * 包名:com.banyou.banyou.util
 * 类描述：
 * 创建人：LX
 * 创建时间：2017/5/23 上午10:50
 * 修改人：LX
 * 修改时间：2017/5/23 上午10:50
 * 修改备注：
 */

public class ShowAddressPicker  {

    public static void show(final BaseActivity activity, final OnCityPickedCallBack callBack){
        final WeakReference<BaseActivity> reference=new WeakReference<BaseActivity>(activity);
        reference.get().showLoadingDialog(activity.LOADING_WHITE);
        Observable.create(new Observable.OnSubscribe<ArrayList<Province>>() {
            @Override
            public void call(Subscriber<? super ArrayList<Province>> subscriber) {
                try {
                    String json = ConvertUtils.toString(reference.get().getAssets().open("citylist.txt"));
                    if (json != null) {
                        CityBean cityBean = JSON.parseObject(json, CityBean.class);
                        ArrayList<Province> data = new ArrayList<>();
                        for (int i = 0; i < cityBean.getProvinceList().size(); i++) {
                            Province province=new Province();
                            province.setAreaName(cityBean.getProvinceList().get(i).getProvinceName());
                            province.setAreaId(cityBean.getProvinceList().get(i).getProvinceCode()+"");
                            List<City> list=new ArrayList<City>();
                            for(int j=0;j<cityBean.getProvinceList().get(i).getCityList().size();j++){
                                City city=new City();
                                city.setAreaName(cityBean.getProvinceList().get(i).getCityList().get(j).getCityName());;
                                city.setProvinceId(cityBean.getProvinceList().get(i).getProvinceCode()+"");
                                city.setAreaId(cityBean.getProvinceList().get(i).getCityList().get(j).getCityCode()+"");
                                list.add(city);
                                CityBean.Province.CityListBean cityListBean = cityBean.getProvinceList().get(i).getCityList().get(j);
                                for(int k=0;k<cityListBean.getTownList().size();k++){
                                    County county=new County();
                                    county.setAreaId(cityListBean.getTownList().get(k).getTownCode()+"");
                                    county.setAreaName(cityListBean.getTownList().get(k).getTownName());
                                    county.setCityId(cityListBean.getCityCode()+"");
                                }
                            }
                            province.setCities(list);
                            data.add(province);
                        }
                        subscriber.onNext(data);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    KLog.a("exception");
                }
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ArrayList<Province>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArrayList<Province> data) {
                        reference.get().hideLoadingDialog();
                        AddressPicker picker = new AddressPicker(reference.get(), data);
                        picker.setHideCounty(true);
                        picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
                            @Override
                            public void onAddressPicked(Province province, City city, County county) {
                                callBack.onAddressPicked(province,city,county);
                            }
                        });
                        if(!picker.isShowing()) {
                            picker.show();
                        }
                    }
                });
    }

    public interface OnCityPickedCallBack{
        void onAddressPicked(Province province, City city, County county);
    }
}
